package eteam.aps.controller;

import eteam.aps.client.CommonClient;
import eteam.aps.common.R;
import eteam.aps.common.exceptions.BusinessException;
import eteam.aps.common.helper.Assert;
import eteam.aps.common.helper.Helper;
import eteam.aps.common.helper.JsonPage;
import eteam.aps.model.dto.ArrivalVouchQueryDto;
import eteam.aps.model.dto.ArrivalVouchUpdateDto;
import eteam.aps.model.vo.arrival.ArrivalVouchShowVo;
import eteam.aps.service.ArrivalVouchService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
// * 到货单;U8到货单 控制层
 *
 * @author LiuLei
 */
@Api(tags = "到货单")
@RestController
@RequestMapping("/avc")
public class ArrivalVouchController {

    @Autowired
    private ArrivalVouchService arrivalVouchService;

    @Autowired
    private CommonClient client;

    @PostMapping("/sbc")
    @ApiOperation("根据条码查询到货单信息")
    @ApiImplicitParam(name = "bar", value = "到货单条码号")
    public R<ArrivalVouchShowVo> selByCode(String bar) throws Exception {
        if (Helper.isEmpty(bar)) throw new BusinessException(HttpStatus.BAD_REQUEST, "到货单条码号不能为空");
        ArrivalVouchShowVo arrivalVouchShowVo = arrivalVouchService.selByBar(bar);
        return R.success(arrivalVouchShowVo);
    }

    @PostMapping("/sbd")
    @ApiOperation("根据到货单单据查询到货单信息")
    @ApiImplicitParam(name = "code", value = "到货单单号")
    public R<List<ArrivalVouchShowVo>> selByDCode(String code) throws Exception {
        if (Helper.isEmpty(code)) throw new BusinessException(HttpStatus.BAD_REQUEST, "到货单单据号不能为空");
        return R.success(arrivalVouchService.selByCode(Helper.filterStrEng(code)));
    }

    @PostMapping("/updateAv")
    @ApiOperation("修改到货单信息")
    @ApiImplicitParam(name = "ArrivalVouchUpdateDto", value = "修改条件")
    public R<Integer> updateAV(@RequestBody List<ArrivalVouchUpdateDto> list) throws Exception {
        if (list.isEmpty()) throw new BusinessException(HttpStatus.BAD_REQUEST, "参数非法异常！");
        return R.success(arrivalVouchService.updateAv(list));
    }

    @PostMapping("/cancelAv")
    @ApiOperation("撤销到货单入库")
    @ApiImplicitParam(name = "ArrivalVouchUpdateDto", value = "修改条件")
    public R<Void> cancelAv(String fId) throws Exception {
        Assert.isBlank(fId, "内码不能为空！");
        arrivalVouchService.cancelAv(fId);
        return R.success();
    }

    @PostMapping("/jsp")
    @ApiOperation("分页条件查询到货单信息")
    @ApiImplicitParam(name = "JsonPage<ArrivalVouchShowVo>", value = "查询条件")
    public R<JsonPage<ArrivalVouchShowVo>> selJSP(@RequestBody ArrivalVouchQueryDto arrivalVouchQueryDto) throws Exception {
        if (arrivalVouchQueryDto == null) throw new BusinessException(HttpStatus.BAD_REQUEST, "参数非法异常！");
        if (Helper.isNotEmpty(arrivalVouchQueryDto.getfCCode()))arrivalVouchQueryDto.setfCCode(Helper.filterStrEng(arrivalVouchQueryDto.getfCCode()));
        JsonPage<ArrivalVouchShowVo> page = arrivalVouchService.selJSP(arrivalVouchQueryDto);
        if (!page.getList().isEmpty()) {
            Map<String, String> userName = client.selectUserName();
            page.getList().forEach(data -> {
                data.setfCreateUserId(userName.getOrDefault(data.getfCreateUserId(), data.getfCreateUserId()));
                data.setfLastModifyUserId(userName.getOrDefault(data.getfLastModifyUserId(), data.getfLastModifyUserId()));
            });
        }
        return R.success(page);
    }

}
